//
//  HotTableViewController.m
//  FreeReading
//
//  Created by lanou3g on 15/11/3.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "HotTableViewController.h"
#import "HotTableViewCell.h"
#import "HotModel.h"
#import "HotManager.h"
#import "AFNetworking.h"
#import "EnjoyDetailViewController.h"
#import "SDCycleScrollView.h"
#import "DJPageView.h"
#import "UIView+LXLFrame.h"
#import "SecondTableViewCell.h"

#define kWidth self.view.frame.size.width
#define kHeight self.view.frame.size.height

@interface HotTableViewController ()<SDCycleScrollViewDelegate>
@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, strong) SDCycleScrollView *scrollView;

@property (nonatomic, strong) NSMutableArray *imageArray;

@property (nonatomic, strong) NSMutableArray *titleArray;

@property (nonatomic, strong) UIView *myView;

@property (nonatomic, strong) UIPickerView *pickerView;
@property (nonatomic, strong) NSArray *pickerData;


@end

@implementation HotTableViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.translucent = NO;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    self.hidesBottomBarWhenPushed = YES;

}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    self.hidesBottomBarWhenPushed = NO;
}

- (NSMutableArray *)allDataArray
{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"热点";
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navBar"] forBarMetrics:(UIBarMetricsDefault)];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"HotTableViewCell" bundle:nil] forCellReuseIdentifier:@"HotTableViewCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"SecondTableViewCell" bundle:nil] forCellReuseIdentifier:@"SecondTableViewCell"];
    
    [self.tableView addLegendHeaderWithRefreshingTarget:self refreshingAction:@selector(setUpData)];
    
    [self.tableView.header beginRefreshing];
    
    self.navigationController.navigationBar.translucent = NO;
    
}

// tabBar隐藏

- (void)setUpData
{
    [[HotManager shareManager] requestDataForReloadWithBlock:^(NSMutableArray *array){
        self.dataArray = array;
        [self.tableView reloadData];
        
        self.titleArray = [NSMutableArray array];
        self.imageArray = [NSMutableArray array];
        for (int i = 0; i < 3; i++) {
            HotModel *model = self.dataArray[i];
            NSString *picUrl = model.picUrl;
            NSString *title = model.title;
            [self.titleArray addObject:title];
            [self.imageArray addObject:picUrl];
        }
        
        self.scrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, kWidth, 200) imageURLStringsGroup:nil]; // 模拟网络延时情景
        _scrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
        _scrollView.delegate = self;
        _scrollView.titlesGroup = self.titleArray;

        _scrollView.dotColor = [UIColor whiteColor]; // 自定义分页控件小圆标颜色
        _scrollView.placeholderImage = [UIImage imageNamed:@"placeholder"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            _scrollView.imageURLStringsGroup = self.imageArray;
        });
        
        self.tableView.tableHeaderView = _scrollView;
        
    }];
    [self.tableView.header endRefreshing];
}

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    EnjoyDetailViewController *enjoyVC = [[EnjoyDetailViewController alloc] init];
    HotModel *model = self.dataArray[index];
    enjoyVC.newsTitle  = model.title;
    enjoyVC.url = model.url;
    enjoyVC.pic_url = model.picUrl;
    enjoyVC.tag = @"3";

    [self.navigationController pushViewController:enjoyVC animated:YES];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count - 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HotModel *model = self.dataArray[indexPath.row + 3];
    if ([model.picUrl isEqualToString:@""]) {
        SecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SecondTableViewCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model = model;
        
        return cell;
    } else {
        HotTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HotTableViewCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model = model;
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    EnjoyDetailViewController *enjoyVC = [[EnjoyDetailViewController alloc] init];
    HotModel *model = self.dataArray[indexPath.row + 3];
    enjoyVC.newsTitle  = model.title;
    enjoyVC.url = model.url;
    enjoyVC.pic_url = model.picUrl;
    enjoyVC.tag = @"3";
    
    [self.navigationController pushViewController:enjoyVC animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

#pragma mark - 内存警告
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    if ([self isViewLoaded] && self.view.window == nil) {
        
        self.view = nil;
    }
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
